
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Node学习(七) --POST文件上传 - 默默默默燃</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="一枚前端搬砖队队员的记录册,简单的文件上传例子处理文件上传数据，也是前后端交互中重要的功能，它的处理方式与数据不同。
接下来，通过一个例子查看服务端接收到的文件上传数据。
首先，在post_file.html中，新建一个用与上,"> 
    <meta name="author" content="张白告丶"> 
    <link rel="alternative" href="atom.xml" title="默默默默燃" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    
<link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">

    
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">

    
<link rel="stylesheet" href="/css/obsidian.css">

    
<link rel="stylesheet" href="/css/ball-atom.min.css">

<meta name="generator" content="Hexo 4.2.0"></head>


<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">默默默默燃</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/favicon.png" alt="" data-url="https://zhanghao-web.github.io">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">Node学习(七) --POST文件上传</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(/img/cover.jpg) ">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/Node"><b>「
                    </b>NODE<b> 」</b></a>
                
                April 04, 2019
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2019/04/04/Node/Node%E5%AD%A6%E4%B9%A0(%E4%B8%83)%20--POST%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/" title="Node学习(七) --POST文件上传" class="">Node学习(七) --POST文件上传</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>Words count</i>
                    24k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>Reading time</i>
                    22 mins.
                </span>
                
                
                
                <span id="busuanzi_container_page_pv">
                    <b class="iconfont icon-read"></b> <i>Read count</i>
                    <span id="busuanzi_value_page_pv">0</span>
                </span>
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list" itemprop="keywords"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/Node/" rel="tag">Node</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s"></span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <h2 id="简单的文件上传例子"><a href="#简单的文件上传例子" class="headerlink" title="简单的文件上传例子"></a>简单的文件上传例子</h2><p>处理文件上传数据，也是前后端交互中重要的功能，它的处理方式与数据不同。</p>
<p>接下来，通过一个例子查看服务端接收到的文件上传数据。</p>
<p>首先，在<code>post_file.html</code>中，新建一个用与上传文件的表单：</p>
<p><code>form</code>的属性<code>enctype=&quot;multipart/form-data&quot;</code>代表表单上传的是文件。</p>
<p><code>enctype</code>的默认值为<code>enctype=&quot;application/x-www-form-urlencoded&quot;</code>表示上传的是数据类型，此时服务端接收到的数据为“username=lee&amp;password=123456&amp;file=upload.txt”。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&lt;form action=<span class="string">"http://localhost:8080/upload"</span> method=<span class="string">"POST"</span> enctype=<span class="string">"multipart/form-data"</span>&gt;</span><br><span class="line">  用户：&lt;input type=<span class="string">"text"</span> name=<span class="string">"username"</span> value=<span class="string">"lee"</span>&gt;<span class="xml"><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span></span><br><span class="line">  密码：&lt;input type=<span class="string">"password"</span> name=<span class="string">"password"</span> value=<span class="string">"123456"</span>&gt;<span class="xml"><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span></span><br><span class="line">  &lt;input type=<span class="string">"file"</span> name=<span class="string">"file"</span> id=<span class="string">""</span>&gt;<span class="xml"><span class="tag">&lt;<span class="name">br</span>/&gt;</span></span></span><br><span class="line">  &lt;input type=<span class="string">"submit"</span> value=<span class="string">"提交"</span>&gt;</span><br><span class="line">&lt;<span class="regexp">/form&gt;</span></span><br></pre></td></tr></table></figure>
<p>其次，在server.js中，查看接收到的表单提交数据：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> server = http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">let</span> arr = []</span><br><span class="line"></span><br><span class="line">  req.on(<span class="string">'data'</span>, (buffer) =&gt; &#123;</span><br><span class="line">    arr.push(buffer)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  req.on(<span class="string">'end'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> buffer = Buffer.concat(arr)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">console</span>.log(buffer.toString())</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">server.listen(<span class="number">8080</span>)</span><br></pre></td></tr></table></figure>
<p>最后，在表单中上传/lesson16/upload.txt文件，并查看打印出的结果：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">------WebKitFormBoundaryL5AGcit70yhKB92Y</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"username"</span></span><br><span class="line"></span><br><span class="line">lee</span><br><span class="line">------WebKitFormBoundaryL5AGcit70yhKB92Y</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"password"</span></span><br><span class="line"></span><br><span class="line"><span class="number">123456</span></span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"file"</span>; filename=<span class="string">"upload.txt"</span></span><br><span class="line">Content-Type: text/plain</span><br><span class="line"></span><br><span class="line">upload</span><br><span class="line">------WebKitFormBoundaryL5AGcit70yhKB92Y--</span><br></pre></td></tr></table></figure>
<h2 id="文件上传数据分析"><a href="#文件上传数据分析" class="headerlink" title="文件上传数据分析"></a>文件上传数据分析</h2><p>通过分析上面这个例子中，服务端接收到的数据，可以得到以下信息：</p>
<p><code>.表单上传的数据，被分隔符“------WebKitFormBoundaryL5AGcit70yhKB92Y”隔开，分隔符在每次上传时都不同。分隔符数据可以从</code>req.headers[‘content-type’]<code>中获取，如：</code>const boundary = ‘–’ + req.headers[‘content-type’].split(‘; ‘)[1].split(‘=’)[1]`。</p>
<p>2.前两段数据中，分别可以获取到表单上传的字段名name=”username”，以及数据“lee”。</p>
<p>3.第三段数据中，多了一个字段filename=”upload.txt”，它表示的是文件的原始名称。以及可以获取到文件类型“Content-Type: text/plain”，表示这是一个文本文件。最后是文件的内容“upload”。</p>
<p>由此可以看出，文件上传数据虽然有些乱，但还是有规律的，那么处理思路就是按照规律，将数据切割之后，取出其中有用的部分。</p>
<h2 id="文件上传数据简化"><a href="#文件上传数据简化" class="headerlink" title="文件上传数据简化"></a>文件上传数据简化</h2><p>先回顾一下上面的数据，并将回车符标记出来：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">------WebKitFormBoundaryL5AGcit70yhKB92Y\r\n</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"username"</span>\r\n</span><br><span class="line">\r\n</span><br><span class="line">lee\r\n</span><br><span class="line">------WebKitFormBoundaryL5AGcit70yhKB92Y\r\n</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"password"</span>\r\n</span><br><span class="line">\r\n</span><br><span class="line"><span class="number">123456</span>\r\n</span><br><span class="line">Content-Disposition: form-data; name=<span class="string">"file"</span>; filename=<span class="string">"upload.txt"</span>\r\n</span><br><span class="line">Content-Type: text/plain\r\n</span><br><span class="line">\r\n</span><br><span class="line">upload\r\n</span><br><span class="line">------WebKitFormBoundaryL5AGcit70yhKB92Y--</span><br></pre></td></tr></table></figure>
<a id="more"></a>
<p>可以看出，每段数据的结构其实是这样的：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">------WebKitFormBoundaryL5AGcit70yhKB92Y\r\nContent-Disposition: form-data; name=<span class="string">"username"</span>\r\n\r\nlee\r\</span><br></pre></td></tr></table></figure>
<p>将每段上传数据简化如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;分隔符&gt;\r\n字段头\r\n\r\n内容\r\n</span><br></pre></td></tr></table></figure>
<p>也就是说，整个表单的数据，就是按照这样的数据格式组装而成。</p>
<p>需要注意的是，在表单数据的结尾不再是\r\n，而是“–”。</p>
<h2 id="文件上传数据处理步骤"><a href="#文件上传数据处理步骤" class="headerlink" title="文件上传数据处理步骤"></a>文件上传数据处理步骤</h2><p>1.用&lt;分隔符&gt;切分数据：</p>
<p>2.删除数组头尾数据：</p>
<p>3.将每一项数据头尾的的\r\n删除：</p>
<p>4.将每一项数据中间的\r\n\r\n删除，得到最终结果：</p>
<h2 id="Buffer的数据处理"><a href="#Buffer的数据处理" class="headerlink" title="Buffer的数据处理"></a>Buffer的数据处理</h2><p>由于文件都是二进制数据，不能直接将其转换为字符串后再进行处理，否则数据会出错，因此要通过Buffer模块进行数据处理操作。</p>
<p>Buffer模块提供了indexOf方法获取Buffer数据中，其参数所在位置的index值。</p>
<p>Buffer模块提供了slice方法，可通过index值切分Buffer数据。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> buffer = Buffer.from(<span class="string">'lee\r\nchen\r\ntest'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> index = buffer.indexOf(<span class="string">'\r\n'</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(index)</span><br><span class="line"><span class="built_in">console</span>.log(buffer.slice(<span class="number">0</span>, index).toString())</span><br></pre></td></tr></table></figure>
<p>可以看到打印结果分别为3和”lee”，也就是说，我们先找到了”\r\n”所在的index为3，之后从Buffer数据的index为0的位置，切割到index为3的位置，得到了正确的结果。</p>
<p>由此，可以封装一个专门用于切割Buffer数据的方法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="keyword">function</span> <span class="title">bufferSplit</span>(<span class="params">buffer, separator</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">let</span> result = [];</span><br><span class="line">  <span class="keyword">let</span> index = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">while</span> ((index = buffer.indexOf(separator)) != <span class="number">-1</span>) &#123;</span><br><span class="line">    result.push(buffer.slice(<span class="number">0</span>, index));</span><br><span class="line">    buffer = buffer.slice(index + separator.length);</span><br><span class="line">  &#125;</span><br><span class="line">  result.push(buffer);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>有了bufferSplit方法，就可以正式开始处理数据了。</p>
<h2 id="文件上传数据处理"><a href="#文件上传数据处理" class="headerlink" title="文件上传数据处理"></a>文件上传数据处理</h2><p>根据上面的思路，就可以实现一个完整的文件上传流程。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>)</span><br><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>)</span><br><span class="line"><span class="keyword">const</span> bufferSplit = <span class="built_in">require</span>(<span class="string">'./bufferSplit'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> server = http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> boundary = <span class="string">`--<span class="subst">$&#123;req.headers[<span class="string">'content-type'</span>].split(<span class="string">'; '</span>)[<span class="number">1</span>].split(<span class="string">'='</span>)[<span class="number">1</span>]&#125;</span>`</span>  <span class="comment">// 获取分隔符</span></span><br><span class="line">  <span class="keyword">let</span> arr = []</span><br><span class="line"></span><br><span class="line">  req.on(<span class="string">'data'</span>, (buffer) =&gt; &#123;</span><br><span class="line">    arr.push(buffer)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  req.on(<span class="string">'end'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">const</span> buffer = Buffer.concat(arr)</span><br><span class="line">    <span class="built_in">console</span>.log(buffer.toString())</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 1. 用&lt;分隔符&gt;切分数据</span></span><br><span class="line">    <span class="keyword">let</span> result = bufferSplit(buffer, boundary)</span><br><span class="line">    <span class="built_in">console</span>.log(result.map(<span class="function"><span class="params">item</span> =&gt;</span> item.toString()))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 2. 删除数组头尾数据</span></span><br><span class="line">    result.pop()</span><br><span class="line">    result.shift()</span><br><span class="line">    <span class="built_in">console</span>.log(result.map(<span class="function"><span class="params">item</span> =&gt;</span> item.toString()))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 3. 将每一项数据头尾的的\r\n删除</span></span><br><span class="line">    result = result.map(<span class="function"><span class="params">item</span> =&gt;</span> item.slice(<span class="number">2</span>, item.length - <span class="number">2</span>))</span><br><span class="line">    <span class="built_in">console</span>.log(result.map(<span class="function"><span class="params">item</span> =&gt;</span> item.toString()))</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 4. 将每一项数据中间的\r\n\r\n删除，得到最终结果</span></span><br><span class="line">    result.forEach(<span class="function"><span class="params">item</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(bufferSplit(item, <span class="string">'\r\n\r\n'</span>).map(<span class="function"><span class="params">item</span> =&gt;</span> item.toString()))</span><br><span class="line"></span><br><span class="line">      <span class="keyword">let</span> [info, data] = bufferSplit(item, <span class="string">'\r\n\r\n'</span>)  <span class="comment">// 数据中含有文件信息，保持为Buffer类型</span></span><br><span class="line"></span><br><span class="line">      info = info.toString()  <span class="comment">// info为字段信息，这是字符串类型数据，直接转换成字符串，若为文件信息，则数据中含有一个回车符\r\n，可以据此判断数据为文件还是为普通数据。</span></span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span> (info.indexOf(<span class="string">'\r\n'</span>) &gt;= <span class="number">0</span>) &#123;  <span class="comment">// 若为文件信息，则将Buffer转为文件保存</span></span><br><span class="line">        <span class="comment">// 获取字段名</span></span><br><span class="line">        <span class="keyword">let</span> infoResult = info.split(<span class="string">'\r\n'</span>)[<span class="number">0</span>].split(<span class="string">'; '</span>)</span><br><span class="line">        <span class="keyword">let</span> name = infoResult[<span class="number">1</span>].split(<span class="string">'='</span>)[<span class="number">1</span>]</span><br><span class="line">        name = name.substring(<span class="number">1</span>, name.length - <span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取文件名</span></span><br><span class="line">        <span class="keyword">let</span> filename = infoResult[<span class="number">2</span>].split(<span class="string">'='</span>)[<span class="number">1</span>]</span><br><span class="line">        filename = filename.substring(<span class="number">1</span>, filename.length - <span class="number">1</span>)</span><br><span class="line">        <span class="built_in">console</span>.log(name)</span><br><span class="line">        <span class="built_in">console</span>.log(filename)</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 将文件存储到服务器</span></span><br><span class="line">        fs.writeFile(<span class="string">`./upload/<span class="subst">$&#123;filename&#125;</span>`</span>, data, err =&gt; &#123;</span><br><span class="line">          <span class="keyword">if</span> (err) &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(err)</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'文件上传成功'</span>)</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;  <span class="comment">// 若为数据，则直接获取字段名称和值</span></span><br><span class="line">        <span class="keyword">let</span> name = info.split(<span class="string">'; '</span>)[<span class="number">1</span>].split(<span class="string">'='</span>)[<span class="number">1</span>]</span><br><span class="line">        name = name.substring(<span class="number">1</span>, name.length - <span class="number">1</span>)</span><br><span class="line">        <span class="keyword">const</span> value = data.toString()</span><br><span class="line">        <span class="built_in">console</span>.log(name, value)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">server.listen(<span class="number">8080</span>)</span><br></pre></td></tr></table></figure>
<h2 id="multiparty"><a href="#multiparty" class="headerlink" title="multiparty"></a>multiparty</h2><p>我们可以尝试使用第三方库来完成POST请求的处理，如<a href="https://github.com/pillarjs/multiparty" target="_blank" rel="noopener">multiparty</a>。</p>
<h3 id="multiparty-demo"><a href="#multiparty-demo" class="headerlink" title="multiparty demo"></a>multiparty demo</h3><p>通过如下例子，可以测试一下multiparty的功能。</p>
<p>它会在field事件中，将数据信息的字段名和值返回。在file事件中，将文件的字段名和信息返回。</p>
<p>上传成功后，会在指定的文件夹创建一个上传的文件，并会将文件重命名（如：IqUHkFe0u2h2TsiBztjKxoBR.jpg），以防止重名。</p>
<p>若上传出现失败，已保存的文件会自动删除。</p>
<p>close事件表示表单数据全部解析完成，用户可以在其中处理已经接收到的信息。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>)</span><br><span class="line"><span class="keyword">const</span> multiparty = <span class="built_in">require</span>(<span class="string">'multiparty'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> server = http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> form = <span class="keyword">new</span> multiparty.Form(&#123;</span><br><span class="line">    uploadDir: <span class="string">'./upload'</span> <span class="comment">// 指定文件存储目录</span></span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  form.parse(req) <span class="comment">// 将请求参数传入，multiparty会进行相应处理</span></span><br><span class="line"></span><br><span class="line">  form.on(<span class="string">'field'</span>, (name, value) =&gt; &#123; <span class="comment">// 接收到数据参数时，触发field事件</span></span><br><span class="line">    <span class="built_in">console</span>.log(name, value)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  form.on(<span class="string">'file'</span>, (name, file, ...rest) =&gt; &#123; <span class="comment">// 接收到文件参数时，触发file事件</span></span><br><span class="line">    <span class="built_in">console</span>.log(name, file)</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  form.on(<span class="string">'close'</span>, () =&gt; &#123;  <span class="comment">// 表单数据解析完成，触发close事件</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'表单数据解析完成'</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">server.listen(<span class="number">8080</span>)</span><br></pre></td></tr></table></figure>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d='undefined'
    >Comments</div>


            
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>张白告丶</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/zhanghao-web" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">149 <p>Articles</p></a></li>
                    <li><a href="/categories">23 <p>Categories</p></a></li>
                    <li><a href="/tags">47 <p>Tags</p></a></li>
                </ul>
            </div>
            
            
            
            <div class="box sticky animated fadeInRight faster">
                <div id="toc" class="subbox">
                    <h4>Contents</h4>
                    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#简单的文件上传例子"><span class="toc-number">1.</span> <span class="toc-text">简单的文件上传例子</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#文件上传数据分析"><span class="toc-number">2.</span> <span class="toc-text">文件上传数据分析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#文件上传数据简化"><span class="toc-number">3.</span> <span class="toc-text">文件上传数据简化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#文件上传数据处理步骤"><span class="toc-number">4.</span> <span class="toc-text">文件上传数据处理步骤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Buffer的数据处理"><span class="toc-number">5.</span> <span class="toc-text">Buffer的数据处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#文件上传数据处理"><span class="toc-number">6.</span> <span class="toc-text">文件上传数据处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#multiparty"><span class="toc-number">7.</span> <span class="toc-text">multiparty</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#multiparty-demo"><span class="toc-number">7.1.</span> <span class="toc-text">multiparty demo</span></a></li></ol></li></ol>
                </div>
            </div>
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2020
        <span class="gradient-text">
            张白告丶
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    
<link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">

    
<script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>


<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>


<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>


<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>


<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>




    
<script src="/js/busuanzi.min.js"></script>

    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>



<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">


<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>




</html>
